<p class="Head1"><help:link Id="66575"><help:productname>%PRODUCTNAME</help:productname> Basic is modular</help:link></p>
<p class="Paragraph">Programming with <help:productname>%PRODUCTNAME</help:productname> Basic is based on single Subs and Functions, which always end with "End Sub" or "End Function". In each Sub or Function, another Subs and Functions can be called. When a macro is executed, it automatically calls the first respective Sub.</p>
</help:to-be-embedded>
<p class="Head2">What is a Sub?</p>
<p class="Paragraph">Sub is a short form for subroutine, i.e. it is a sub-procedure, which handle a certain task within another program. Split the task you want to program into individual procedures, then split each procedure into the steps you need for the Sub. This means that the entire program should be split into hierarchically ordered procedures and sub-procedures.</p>
<p class="Head2">What is a Function?</p>
<p class="Paragraph">A function is essentially a procedure, which directly returns a value. You may use a function at the right side of a variable declaration, or at other places where you normally use values.</p>
<p class="Paragraph">The more common you code a Sub or Function, the higher is the probability that you can re-use it in other programs. If your code tests text entries for a length of more than 6 characters, then you should write a function that utilizes the upper limit 6 as a parameter. This way you can use the same function to test a text entry for, e.g., more than 20 characters.</p>
<p class="Head2">Global and local variables</p>
<p class="Paragraph">In <help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic you can declare variables that are valid globally for all subs. Also variables are possible that are not explicitly declared in a Dim statement; <help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic declares these variables internally. See also <help:link Id="66513" xmlns:help="http://openoffice.org/2000/help">Procedures and Functions</help:link>.</p>
<p class="Paragraph">Variables that you declare within a sub or function are valid only inside this sub or function. These variables have priority over possible variables with the same name in higher subs, and over global variables that are valid only after the sub has ended. With this exception, global variables are valid everywhere else in the program.</p>
<p class="Head2">Program structuring</p>
<p class="Paragraph">After you have broke down your program into procedures and functions (Subs and Functions), you may want to save matching procedures and functions as files, so you can use them in other projects. <help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic supports this by using <help:link Id="66460" xmlns:help="http://openoffice.org/2000/help">Modules and Libraries</help:link>. All subs and functions are always contained in modules, and you may use an almost endless number of modules. You can define modules to be global or part of a document. Multiple modules can be combined to a library, and there is no limit to the number of libraries.</p>
<p class="Paragraph">You can copy or move single subs and functions and even modules and libraries from one file to another file. Find more information in the dialog <help:link Id="68327" xmlns:help="http://openoffice.org/2000/help">Macro</help:link>.</p>